
Ext.define('Admin.view.main.MainController', {
    extend: 'Ext.app.ViewController',

    alias: 'controller.main',

    // onToggleConfig: function (menuitem) {
    //     var treelist = this.lookupReference('treelist');
    // },
    /**
     * 处理路由
     */
    listen : {
        controller : {
            //未匹配的路由都到这里来
            '#' : {
                unmatchedroute : 'otherPage'
            }
        }
    },
    routes : {
        ':node' : {
            // before  : 'onBeforeUser',
            action  : 'onMain'
        }
    },

    onMain : function (hashTag) {
        // hashTag = (hashTag || '').toLowerCase();
        hashTag = hashTag || ''
        if(hashTag == 'main-view') hashTag = 'homepage-view'
        let mainRef = this.getReferences().re_main_main_wrapper
        let mainCard = mainRef.lookupReference('centerContainer')
        let mainLayout = mainCard.getLayout()
        // debugger
        let navigationList = mainRef.lookupReference('navigationTreeList')
        let existingItem = mainCard.child('component[routeId=' + hashTag + ']')
        // let navigationStore = navigationList.getStore()
        // let routeNode = navigationStore.findNode('routeId', hashTag)
        let view = hashTag
        // let title = ''
        //判断路由是否存在,不存在则跳转到404界面
        let routeList = []
        this.getViewModel().getData().navItems.getData().each(item => {
            routeList.push(...Ext.create('Admin.ux.util.Util').treeChangelist([item.data]))
        })
        if(routeList.map(item => item.routeId).indexOf(hashTag) == -1){
            // title = '4 0 4 !'
            view = 'page404-view';
        }
        // debugger
        // title = routeList[routeList.map(item => item.routeId).indexOf(hashTag)].text
        // debugger
        //销毁之前的所有组件,然后重新加载新组件
        // mainCard.getRefItems().forEach(i => i.destroy())
        mainCard.items.items.forEach(i => i.destroy())

        // navigationList.destroy()
        //重新绘创建新界面,覆盖右侧主界面
        let newView = existingItem ? existingItem : Ext.create({
            xtype: view,
            routeId: hashTag,  // for existingItem search later
            // title : title,
            // hideMode: 'offsets'
            hideMode: 'display'
        });
        Ext.suspendLayouts();
        // mainCard.suspendLayouts()
        // mainLayout.setActiveItem(mainCard.add(newView));
        mainCard.add(newView)
        // mainCard.show()
        // mainLayout.setActiveItem(newView);
        Ext.resumeLayouts(true);
        // mainCard.resumeLayouts(true);
        // console.log('====')
    },
    otherPage : function(){

    },
    /**
     * 自适应屏幕
     * @param own
     * @param width
     * @param height
     * @param oldWidth
     * @param oldHeight
     * @param eOpts
     */
    onResize :function (own, width, height, oldWidth, oldHeight, eOpts) {
        // let panel = own.query('main-main-wrapper')[0]
        let ref = this.getReferences()
        let panel = ref.re_main_main_wrapper
        let rightChildPanel = panel.lookupReference('centerContainer')
        //主界面自适应屏幕
        panel.setWidth(width)
        panel.setHeight(height)
        //右侧容器自适应屏幕;
        // rightChildPanel.getBubbleParent().setWidth(rightChildPanel.getBubbleParent().getWidth() - 50)
        // rightChildPanel.getBubbleParent().setHeight(rightChildPanel.getBubbleParent().getHeight() - 50)
        rightChildPanel.setWidth(rightChildPanel.getBubbleParent().getWidth() - 20)
        rightChildPanel.setHeight(rightChildPanel.getBubbleParent().getHeight() - 20)
    },

    /**
     * 切换左侧按钮的显示样式,折叠或展开
     * @param button
     * @param pressed
     */
    onToggleMicro: function (button, pressed) {
        // let model1 = this.lookupViewModel()
        let model = this.getViewModel()
        let ref = this.getReferences().re_main_main_wrapper
        var treelist = ref.lookupReference('treelist'),
            navBtn = ref.lookupReference('navBtn'),
            centerContainer = ref.lookupReference('centerContainer'),
            ct = treelist.ownerCt

        treelist.setMicro(pressed);

        if (pressed) {
            navBtn.setPressed(true);
            navBtn.disable();
            this.oldWidth = ct.width;
            ct.setMinWidth(0)
            ct.setWidth(44);
        } else {
            ct.setMinWidth(model.westMinWidth)
            ct.setWidth(model.westBaseWidth)
            centerContainer.updateLayout()
            ct.setWidth(this.oldWidth);
            navBtn.enable();
        }

        if (Ext.isIE8) {
            this.repaintList(treelist, pressed);
        }
    },
    /**
     * 通过左侧选中的node,跳转路由
     * @param tree
     * @param node
     */
    onNavigationTreeSelectionChange : function(tree, node){
        // console.log(tree)
        // console.log(node)
        if(!tree.getSelection()) return
        let selectData = tree.getSelection().getData()
        let routeId = selectData.routeId
        this.redirectTo(routeId)
    }

});

